package com.lagou.homework.sort;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class SortFileNumDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        // 1.获取配置文件对象，获取job对象实例
        final Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "SortFileNumDriver");

        // 2.指定程序jar的本地路径
        job.setJarByClass(SortFileNumDriver.class);

        // 3.指定 Mapper/Reduce 类
        job.setMapperClass(SortFileNumMapper.class);
        job.setReducerClass(SortFileNumReducer.class);

        // 4.指定Mapper输出的kv数据类型
        job.setMapOutputKeyClass(LongWritable.class);
        job.setMapOutputValueClass(NullWritable.class);

        // 5.指定最终输出的kv数据类型
        job.setOutputKeyClass(LongWritable.class);
        job.setOutputValueClass(LongWritable.class);

        // 6.指定job处理的原始数据路径
        // /Users/pengxixi/JavaSource/lagou/lagou-home-work/code/Hadoop框架核心作业/SortFileNum/file
        // /Users/sumscope/Desktop/lagou-home-work/code/Hadoop框架核心作业/SortFileNum/file
        FileInputFormat.setInputPaths(job,new Path("/Users/pengxixi/JavaSource/lagou/lagou-home-work/code/Hadoop框架核心作业/SortFileNum/file"));   // 指定读取数据的原始路径
        // 7.指定Job输出结果路径
        FileOutputFormat.setOutputPath(job,new Path("/Users/pengxixi/JavaSource/lagou/lagou-home-work/code/Hadoop框架核心作业/SortFileNum/file/output"));

        // 8.提交作业
        boolean b = job.waitForCompletion(true);
        // jvm退出：正常退出0，非0值则是错误退出
        System.exit(b == true ? 0 : 1);
    }
}
